
#include "../libpatches.h"
#include "randh.h"
#include <stdio.h>
#include <stdlib.h>
#include <float.h>

static void module_randh_processor(module m);
void module_randh_customizer(module m, char * name, char * value);

void module_randh_initializer(module m) {
	m->customizer = module_randh_customizer;
}

void module_randh_customizer(module m, char * name, char * value) {

	customizer_count_inputs_only(m, name, value);
	if (m->input_count && ! m->inputs) {
		make_outputs(m, m->input_count);
		make_inputs(m, 1);
		make_buffer(m, 1);
		m->input_names[0] = "in";

		m->buffer[0] = DBL_MAX;
		m->processor = module_randh_processor;
	} 
}

static void module_randh_processor(module m) {
	input p = m->named_inputs[0];
	sample * last = m->buffer;
	output * o;
	int c;

	if (**p < *last)
		for (
			o = m->outputs, 
			c = m->output_count;
			c;
			--c, ++o
		)
			**o = rand() / (double) RAND_MAX;

	*last = **p;
}
